package com.linzhaowei.headhuntingservice;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.linzhaowei.headhuntingservice.adapter.JobObjectiveAdapter;
import com.linzhaowei.headhuntingservice.bean.JobObjective;
import com.linzhaowei.headhuntingservice.utils.HttpUtils;
import com.linzhaowei.headhuntingservice.utils.Ip;

import java.util.ArrayList;
import java.util.List;

public class Receive extends AppCompatActivity {

    RecyclerView recyclerView;
    List<JobObjective> jobObjectiveList=new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receive);
        initViews();

        getReceive();

    }

    private void initViews(){
        recyclerView=findViewById(R.id.receive_list);
    }

    void getReceive(){
        new Thread(() -> {
            HttpUtils httpUtils=new HttpUtils();
            try{
                SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
                String result=httpUtils.get(Ip.ip+"/user/getReceive?userId="+sharedPreferences.getInt("id", -1));
                JSONArray jsonArray= JSON.parseArray(result);
                for(int i=0;i<jsonArray.size();i++){
                    jobObjectiveList.add(jsonArray
                            .getJSONObject(i)
                            .toJavaObject(JobObjective.class));
                }

                runOnUiThread(this::show);

            }catch (Exception e){
                e.printStackTrace();
            }
        }).start();
    }

    void show() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        JobObjectiveAdapter adapter = new JobObjectiveAdapter(jobObjectiveList);
        recyclerView.setAdapter(adapter);
    }
}
